<?php
/*IMPORTANT: 注意检查文件字符集是否为UTF-8*/
/*NOTICE: image类需要GD2库支持，请确保在php.ini的extension中已开启了GD2*/
/*NOTICE: 在服务器启用后切勿修改avartar.conf.php中除了highQuality外的参数，否则将可能导致用户头像加载失败等严重错误*/
/*NOTICE: 请谨慎开启avartar.conf.php中highQuality功能，这将导致服务器负载增加甚至在极限情况下超载，并增加服务器存储开销*/
/**
 *
 * @application epintx
 * @author Jiong
 * @copyright www.epintx.com
 * 
 */
/**
 *
 * @当前image支持：JPEG PNG 暂时不支持BMP
 * @作用：模拟HTTP访问操作访问特定资源
 *
 */
class image
{
	public $height;
	public $width;
	public $type;
	public function __construct()
	{
		;
	}
	public function __destruct()
	{
		;
	}
	public function ImageConvert($res_file,$dest_file,$res_type,$dest_type,$sizex,$sizey)
	{
		if($res_type=="jpeg" && $dest_type=="png")
		{
			$this->JpegToPng($res_file,$dest_file,$sizex,$sizey);
		}
		else if($res_type=="png" && $dest_type=="png")
		{
			$this->PngToPng($res_file,$dest_file,$sizex,$sizey);
		}
		else if($res_type=="jpeg" && $dest_type=="jpeg")
		{
			$this->JpegToJpeg($res_file,$dest_file,$sizex,$sizey);
		}
		else if($res_type=="png" && $dest_type=="jpeg")
		{
			$this->PngToJpeg($res_file,$dest_file,$sizex,$sizey);
		}
		else
		{
			move_uploaded_file($res_file,$dest_file);
		}

	}
	private function JpegToPng($res_file,$dest_file,$sizex,$sizey)
	{
		$res_img = imagecreatefromjpeg($res_file);
		$res_img = $this->PngResize($res_img,$size);
		imagecolortransparent($res_img,imagecolorallocate($newimg, 0, 0, 0));
		imagePng($res_img,$dest_file);
		imagedestroy($res_img);
		unset($res_img);
	}
	private function PngToPng($res_file,$dest_file,$sizex,$sizey)
	{
		$res_img = imagecreatefrompng($res_file);
		$res_img = $this->PngResize($res_img,$size);
		imagecolortransparent($res_img,imagecolorallocate($res_img, 0, 0, 0));
		imagePng($res_img,$dest_file);
		imagedestroy($res_img);
		unset($res_img);
	}
	private function JpegToJpeg($res_file,$dest_file,$sizex,$sizey)
	{
		$res_img = imagecreatefromjpeg($res_file);
		$res_img = $this->JpegResize($res_img,$sizex,$sizey);
		imageJpeg($res_img,$dest_file);
		imagedestroy($res_img);
		unset($res_img);
	}
	private function PngToJpeg($res_file,$dest_file,$sizex,$sizey)
	{
		$res_img = imagecreatefrompng($res_file);
		$res_img = $this->JpegResize($res_img,$sizex,$sizey);
		imageJpeg($res_img,$dest_file);
		imagedestroy($res_img);
		unset($res_img);
	}
	private function PngResize($curimg,$sizex,$sizey)
	{
		$curx = imagesx($curimg);
		$cury = imagesy($curimg);
		$newimg = imagecreatetruecolor(256,256);
		imagecopyresized($newimg,$curimg,0,0,0,0,$sizex,$sizey,$curx,$cury);
		return $newimg;
	}
	private function JpegResize($curimg,$sizex,$sizey)
	{
		$curx = imagesx($curimg);
		$cury = imagesy($curimg);
		$newimg = imagecreatetruecolor($sizex,$sizey);
		imagefill($newimg,0,0,imagecolorallocate($newimg, 255, 255, 255));
		imagecopyresized($newimg,$curimg,0,0,0,0,$sizex,$sizey,$curx,$cury);
		return $newimg;
	}
	public function GetImageType($type)
	{
		$imgtype="";
		switch ($type)
		{
			case "image/x-png":
				$imgtype = "png";
				break;
			case "image/png":
				$imgtype = "png";
				break;
			case "image/gif":
				$imgtype = "gif";
				break;
			case "image/jpeg":
				$imgtype = "jpeg";
				break;
			case "image/pjpeg":
				$imgtype = "jpeg";
				break;
			default:
				$imgtype = "Unknown";
		}
		return $imgtype;
	}
}
class avartar extends image
{
	public $user;
	public function __construct()
	{
		;
	}
	public function __destruct()
	{
		;
	}
	public function put($userid,$file)
	{
		include ('./config/avartar.conf.php');
		$this->type = $this->GetImageType($file["type"]);
		$path_base = "./static/avartar/".StaticFuncAvartarPartitionAssign($userid)."/";
		$filename_encoded = sha1($userid.date("Ymdhis").rand(1000,9999)).".jpg";
		$sizepre_large = $avartarConfig['largePre'];
		$sizepre_medium = $avartarConfig['mediumPre'];
		$sizepre_small = $avartarConfig['smallPre'];
		$sizex_large = $avartarConfig['largeX'];
		$sizey_large = $avartarConfig['largeY'];
		$sizex_medium = $avartarConfig['mediumX'];
		$sizey_medium = $avartarConfig['mediumY'];
		$sizex_small = $avartarConfig['smallX'];
		$sizey_small = $avartarConfig['smallY'];
		$this->save($file["tmp_name"], $path_base.$sizepre_large.$filename_encoded, $this->type, "jpeg", $sizex_large, $sizey_large);
		$this->save($file["tmp_name"], $path_base.$sizepre_medium.$filename_encoded, $this->type, "jpeg", $sizex_medium, $sizey_medium);
		$this->save($file["tmp_name"], $path_base.$sizepre_small.$filename_encoded, $this->type, "jpeg", $sizex_small, $sizey_small);
		return $filename_encoded;
	}
	public function get($size)//$size={"small","medium","large"}
	{
		;
	}
	public function getpath($size)//$size={"small","medium","large"}
	{
		;
	}
	private function save($filename, $savepath, $file_type, $save_type, $sizex, $sizey)
	{
		$this->ImageConvert($filename, $savepath, $file_type, $save_type, $sizex, $sizey);
	}
}
class captcha extends image
{
	public $length;
	private $string;
	public function __construct()
	{
		;
	}
	public function __destruct()
	{
		;
	}
	public function setlength($len)
	{
		$this->length = $len;
	}
	private function randnumToChar($ascii)
	{
		$char='a';
		$rand=rand(1,2);
		if($rand==2)
		{
			$char='A';
		}
		for($i=1;$i<$ascii;++$i)
		{
			++$char;
		}
		if($ascii>26)
		{
			$char=36-$ascii;
		}
		return $char;
	}
	public function generate()
	{
		require_once ('./config/captcha.conf.php');
		$im = imagecreate(20*$this->length,26);
		//$im2 = imagecreate(20*$this->length,34);
		$back = ImageColorAllocate($im, rand(220,255),rand(220,255),rand(220,255));
		imagefill($im,0,0,$back);
		$vcodes = "";
		for($i=0;$i<$this->length;$i++)
		{
			$fontcolor = ImageColorAllocate($im, rand(0,170),rand(0,170),rand(0,170));
			$authnum=rand(1,36);
			$authnum=$this->randnumToChar($authnum);
			if($authnum=='0'||$authnum=='O'||$authnum=='q'||$authnum=='9'||$authnum=='l'||$authnum=='1')
				$i--;
			else
			{
				$vcodes.=$authnum;
				$hrand=rand(-2,4);
				//imagestring($im, 5, 2+$i*10, $hrand, $authnum, $fontcolor);
				$ttf = $captchaConfig["font"];
				imagettftext($im, $captchaConfig["size"], rand(-15,15), 0+$i*20, 19+rand(-1,1),  $fontcolor, $ttf, $authnum);
			}
		}
		for($i=0;$i<300;$i++)
		{ 
			$randcolor = ImageColorallocate($im,rand(0,222),rand(0,222),rand(0,222));
			imagesetpixel($im, rand(1,100)%100 , rand(1,100)%70 , $randcolor);
		} 
		//imagecopyresized($im2, $im, 0, 0, 0, 0, 20*$this->length, 34, 10*$this->length, 17);
		$vcodes = strtolower($vcodes);
		header("Content-type: ".$captchaConfig["contentType"]);
		ImagePNG($im);
		ImageDestroy($im);
		//ImageDestroy($im2);
		return $vcodes;
	}
}
?>